#include "splview.h"
#include <QResizeEvent>
#include "spl.h"
#include <QPainter>

SplView::SplView(QWidget *parent)
	: QWidget(parent)
{
    background.load(":/images/Theater.png");
    speaker.load(":/images/Sound.png");
	setAutoFillBackground(true);
// 	s = new spl(speaker, this);
// 	s->setAngle(270);
// 	s->setText("76", Qt::green);
// 	s->setGeometry(60, 60, 40, 40);
// 	s->show();
}

SplView::~SplView()
{
	while(splList.size()>0)
	{
		delete splList.back();
		splList.pop_back();
	}
}

void SplView::resizeEvent(QResizeEvent * event)
{
	QWidget::resizeEvent(event);
	QPalette pal(palette());
	pal.setBrush(QPalette::Window,QBrush(background.scaled(event->size(), Qt::IgnoreAspectRatio,Qt::SmoothTransformation)));
	setPalette(pal);
    moveSpeaker();
	for(int i = 0; i < splList.size(); i++)
		splList.at(i)->reDraw();
}

void SplView::reDraw()
{
    for(int i = 0; i < splList.size(); i++)
        splList.at(i)->reDraw();
}

// void SplView::paintEvent(QPaintEvent *e)
// {
// 	s->paintEvent(e);
// }

void SplView::setAudioCounts(int counts)
{
	while(splList.size()>0)
	{
		delete splList.back();
		splList.pop_back();
	}
	spl* s;
	switch(counts)
	{
	case 6:
		//Left
		s = new spl(speaker, this);
		s->setAngle(90);
		s->setGeometry(20*this->width()/561, 30, 40, 40);
		splList.push_back(s);
		//Right
		s = new spl(speaker, this);
		s->setAngle(90);
		s->setGeometry((561 - 40)*this->width()/561 - 20, 30, 40, 40);
		splList.push_back(s);
		//Center
		s = new spl(speaker, this);
		s->setAngle(90);
		s->setGeometry(280*this->width()/561 - 20, 30, 40, 40);
		splList.push_back(s);
		//LFE
		s = new spl(speaker, this);
		s->setAngle(90);
		s->setGeometry(380*this->width()/561 - 20, 30, 40, 40);
		splList.push_back(s);
		//LS
		s = new spl(speaker, this);
		s->setAngle(270);
		s->setGeometry(20*this->width()/561, 320*this->height()/321 - 40, 40, 40);
		splList.push_back(s);
		//RS
		s = new spl(speaker, this);
		s->setAngle(270);
		s->setGeometry((561 - 40)*this->width()/561 - 20, 320*this->height()/321 - 40, 40, 40);
		splList.push_back(s);
		break;
	case 8:
		//Left
		s = new spl(speaker, this);
		s->setAngle(90);
		s->setGeometry(20*this->width()/561, 30, 40, 40);
		splList.push_back(s);
		//Right
		s = new spl(speaker, this);
		s->setAngle(90);
		s->setGeometry((561 - 40)*this->width()/561 - 20, 30, 40, 40);
		splList.push_back(s);
		//Center
		s = new spl(speaker, this);
		s->setAngle(90);
		s->setGeometry(280*this->width()/561 - 20, 30, 40, 40);
		splList.push_back(s);
		//LFE
		s = new spl(speaker, this);
		s->setAngle(90);
		s->setGeometry(380*this->width()/561 - 20, 30, 40, 40);
		splList.push_back(s);
		//LS
		s = new spl(speaker, this);
		s->setAngle(270);
		s->setGeometry(20*this->width()/561, 320*this->height()/321 - 40, 40, 40);
		splList.push_back(s);
		//RS
		s = new spl(speaker, this);
		s->setAngle(270);
		s->setGeometry((561 - 40)*this->width()/561 - 20, 320*this->height()/321 - 40, 40, 40);
		splList.push_back(s);
		//LL
		s = new spl(speaker, this);
		s->setAngle(0);
		s->setGeometry(20*this->width()/561, 180*this->height()/321 - 40, 40, 40);
		splList.push_back(s);
		//RL
		s = new spl(speaker, this);
		s->setAngle(180);
		s->setGeometry((561 - 40)*this->width()/561 - 20, 180*this->height()/321 - 40, 40, 40);
		splList.push_back(s);
		break;
	}
}

void SplView::setAudioValue(QString value)
{
	QStringList strList = value.split("|");
	for (int i = 0; i < splList.size(); i++)
	{
		splList.at(i)->setText(strList.at(0), Qt::green);
	}
}

void SplView::setAudioValue(float value, int channel, const QColor color)
{
	if (splList.at(channel) != NULL)
	{
		splList.at(channel)->setText(QString::number(value), color);
	}
}

void SplView::moveSpeaker()
{
    switch(splList.size())
    {
    case 6:
        //Left
        splList.at(0)->setGeometry(20*this->width()/561, 30, 40, 40);
        //Right
        splList.at(1)->setGeometry((561 - 40)*this->width()/561 - 20, 30, 40, 40);
        //Center
        splList.at(2)->setGeometry(280*this->width()/561 - 20, 30, 40, 40);
        //LFE
        splList.at(3)->setGeometry(380*this->width()/561 - 20, 30, 40, 40);
        //LS
        splList.at(4)->setGeometry(20*this->width()/561, 320*this->height()/321 - 40, 40, 40);
        //RS
        splList.at(5)->setGeometry((561 - 40)*this->width()/561 - 20, 320*this->height()/321 - 40, 40, 40);
        break;
    case 8:
        //Left
        splList.at(0)->setGeometry(20*this->width()/561, 30, 40, 40);
        //Right
        splList.at(1)->setGeometry((561 - 40)*this->width()/561 - 20, 30, 40, 40);
        //Center
        splList.at(2)->setGeometry(280*this->width()/561 - 20, 30, 40, 40);
        //LFE
        splList.at(3)->setGeometry(380*this->width()/561 - 20, 30, 40, 40);
        //LS
        splList.at(4)->setGeometry(20*this->width()/561, 320*this->height()/321 - 40, 40, 40);
        //RS
        splList.at(5)->setGeometry((561 - 40)*this->width()/561 - 20, 320*this->height()/321 - 40, 40, 40);
        //LL
        splList.at(6)->setGeometry(20*this->width()/561, 180*this->height()/321 - 40, 40, 40);
        //RL
        splList.at(7)->setGeometry((561 - 40)*this->width()/561 - 20, 180*this->height()/321 - 40, 40, 40);
        break;
    }
}
